package com.sgb.kjwl.viewmodel.packers;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.packers.PackersFinanceEntity;
import com.sgb.kjwl.model.entity.supplier3.DictEntity3;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.activity.packers.PackersFinanceActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
*@author 毁灭霸者
*@date 2020/7/1
*description:
*/
public class PackersFinanceModel extends BaseViewModel {
    public ObservableField<Map<String,Object>> map = new ObservableField<>(new HashMap<>());
    public ObservableField<DictEntity3> dictEntity3 = new ObservableField<>(new DictEntity3());
    private PackersFinanceActivity activity;
    private HomeRepertory repertory;
    private int page = 1;
    public int total;

    public PackersFinanceModel(PackersFinanceActivity activity, HomeRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
        dictEntity3.get().setName("全部");
        dictEntity3.get().setId("-1");
    }

    public void getLegalListModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        map.get().put("page", page);
        int size = 10;
        map.get().put("limit", size);
        map.get().put("areacode", repertory.getAreaCode());
        APIManager.getInstance().getPackerAPI().getPackersFinanceList(map.get())
                .enqueue(new Callback<BaseEntity<ListResponse<List<PackersFinanceEntity>>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<ListResponse<List<PackersFinanceEntity>>>> call, Response<BaseEntity<ListResponse<List<PackersFinanceEntity>>>> response) {
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showList(response.body().getData().getList(), isLoadMore);
                        } else {
                            assert response.body() != null;
                            MToast.showToast(activity, response.body().getMsg());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<ListResponse<List<PackersFinanceEntity>>>> call, Throwable t) {

                    }
                });
    }

    public void getFilterType() {
        APIManager.getInstance().getPackerAPI().getFinanceFilterType(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showTypePopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    public void getFilterArea() {
        APIManager.getInstance().getCommonAPI().getAreaFilterByCode(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<DictEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<DictEntity3>>> call, Response<BaseEntity<List<DictEntity3>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    activity.showAreaPopWindow(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<DictEntity3>>> call, Throwable t) {

            }
        });
    }

    public void getH5Url(String id, String no){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("FinanceTeamDefault").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    String url = response.body().getData().getUrl() + "?id=" + id + "&no=" + no + "&areacode=" + repertory.getAreaCode()+"&status=1";
                    WebViewX5HostActivity.start(activity,url);
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
